package com.info.zhiduoduo.core.service.impl;

import com.info.zhiduoduo.common.constants.invoice.InvoiceType;
import com.info.zhiduoduo.common.utils.NumberUtil;
import com.info.zhiduoduo.core.service.InvoiceInventoryService;
import com.info.zhiduoduo.repository.entity.invoice.CompanyInventory;
import com.info.zhiduoduo.repository.entity.invoice.CompanyInventoryRecord;
import com.info.zhiduoduo.repository.entity.invoice.InvoiceInventory;
import com.info.zhiduoduo.repository.entity.invoice.InvoiceInventoryRecord;
import com.info.zhiduoduo.repository.mapper.invoice.CompanyInventoryRecordMapper;
import com.info.zhiduoduo.repository.mapper.invoice.InvoiceInventoryRecordMapper;

import org.springframework.stereotype.Service;

import java.util.Date;

import javax.annotation.Resource;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@Service
public class InvoiceInventoryServiceImpl implements InvoiceInventoryService {

	@Resource
	private InvoiceInventoryRecordMapper invoiceInventoryRecordMapper;
	@Resource
	private CompanyInventoryRecordMapper companyInventoryRecordMapper;

	@Override
	public void recordInventoryIn(int id) {
		InvoiceInventoryRecord inventoryRecord = new InvoiceInventoryRecord().selectById(id);
		if (inventoryRecord == null) return;
		if (inventoryRecord.getState() == 2) return;

		Integer invoiceType = inventoryRecord.getInvoiceType();
		Integer amountType = inventoryRecord.getAmountType();
		Integer amount = inventoryRecord.getAmount();
		Integer invoiceSum = 0;
		InvoiceInventory inventory = new InvoiceInventory().selectById(inventoryRecord.getUserid());
		if (inventory == null) {
			inventory = new InvoiceInventory().setUserid(inventoryRecord.getUserid());
		}

		if (invoiceType == 1) {
			if (amountType == 1) {
				invoiceSum = NumberUtil.safeInt(inventory.getGenTenThousand()) + amount;
				inventory.setGenTenThousand(invoiceSum);
			} else {
				invoiceSum = NumberUtil.safeInt(inventory.getGenHundredThousand()) + amount;
				inventory.setGenHundredThousand(invoiceSum);
			}
		} else {
			if (amountType == 1) {
				invoiceSum = NumberUtil.safeInt(inventory.getSpeTenThousand()) + amount;
				inventory.setSpeTenThousand(invoiceSum);
			} else {
				invoiceSum = NumberUtil.safeInt(inventory.getSpeHundredThousand()) + amount;
				inventory.setSpeHundredThousand(invoiceSum);
			}
		}

		if (inventory.insertOrUpdate()) {
			inventoryRecord.setState(2).setInvoiceSum(invoiceSum).setEntryTime(new Date()).updateById();
		}
	}

	@Override
	public void recordInventoryOut(int tenThousand, int hundredThousand, int userid, int invoiceType, int adminid, int invoiceId,
	                               int projectType) {
		// 更新发票库存
		InvoiceInventory inventory = new InvoiceInventory().selectById(userid);
		if (inventory == null) return;

		int tenThousandInvoiceSum = 0;
		int hundredTHousandInvoiceSum = 0;
		if (invoiceType == InvoiceType.GENERAL) {
			// 普票
			tenThousandInvoiceSum = inventory.getGenTenThousand() - tenThousand;
			hundredTHousandInvoiceSum = inventory.getGenHundredThousand() - hundredThousand;
			inventory.setGenTenThousand(tenThousandInvoiceSum);
			inventory.setGenHundredThousand(hundredTHousandInvoiceSum);
		} else if (invoiceType == InvoiceType.SPECIAL) {
			// 专票
			tenThousandInvoiceSum = inventory.getSpeTenThousand() - tenThousand;
			hundredTHousandInvoiceSum = inventory.getSpeHundredThousand() - hundredThousand;
			inventory.setSpeTenThousand(tenThousandInvoiceSum);
			inventory.setSpeHundredThousand(hundredTHousandInvoiceSum);
		} else {
			return;
		}
		inventory.updateById();

		// 更新出库记录
		if (tenThousand > 0) {
			InvoiceInventoryRecord record = new InvoiceInventoryRecord().setUserid(userid)
					.setAdminid(adminid)
					.setEntryTime(new Date())
					.setInvoiceType(invoiceType)
					.setAmount(tenThousand)
					.setAmountType(1)
					.setInvoiceId(invoiceId)
					.setProjectType(projectType)
					.setInvoiceSum(tenThousandInvoiceSum)
					.setState(3);

			record.insert();
		}
		if (hundredThousand > 0) {
			InvoiceInventoryRecord record = new InvoiceInventoryRecord().setUserid(userid)
					.setAdminid(adminid)
					.setEntryTime(new Date())
					.setInvoiceType(invoiceType)
					.setAmount(hundredThousand)
					.setAmountType(2)
					.setInvoiceSum(hundredTHousandInvoiceSum)
					.setInvoiceId(invoiceId)
					.setProjectType(projectType)
					.setState(3);
			record.insert();
		}
	}

	@Override
	public void oepnTaxAcctFirstInventoryIn(Integer userid, int adminid) {
		InvoiceInventoryRecord record = new InvoiceInventoryRecord().setUserid(userid)
				.setAdminid(adminid)
				.setInvoiceType(InvoiceType.GENERAL)
				.setAmount(25)
				.setAmountType(1)
				.setState(1);

		record.insert();
	}

	@Override
	public void recordCompanyInventoryIn(int id) {
		CompanyInventoryRecord inventoryRecord = new CompanyInventoryRecord().selectById(id);
		if (inventoryRecord == null) return;
		// 防止发票重复入库
		if (2 == inventoryRecord.getState()) return;

		Integer amountType = inventoryRecord.getAmountType(); // 发票版本
		Integer invoiceType = inventoryRecord.getInvoiceType(); //发票类型
		Integer amount = inventoryRecord.getInvoiceNum(); // 发票数量
		Integer invoiceSum = 0;

		CompanyInventory inventory = new CompanyInventory().selectById(inventoryRecord.getCompanyId());
		if (inventory == null) {
			inventory = new CompanyInventory().setCompanyId(inventoryRecord.getCompanyId());
		}

		if (1 == invoiceType) {
			if (1 == amountType) {
				invoiceSum = NumberUtil.safeInt(inventory.getGenTenThousand()) + amount;
				inventory.setGenTenThousand(invoiceSum);
			} else {
				invoiceSum = NumberUtil.safeInt(inventory.getGenHundredThousand()) + amount;
				inventory.setGenHundredThousand(invoiceSum);
			}
		} else {
			if (amountType == 1) {
				invoiceSum = NumberUtil.safeInt(inventory.getSpeTenThousand()) + amount;
				inventory.setSpeTenThousand(invoiceSum);
			} else {
				invoiceSum = NumberUtil.safeInt(inventory.getSpeHundredThousand()) + amount;
				inventory.setSpeHundredThousand(invoiceSum);
			}
		}

		if (inventory.insertOrUpdate()) {
			inventoryRecord.setState(2).setInvoiceSum(invoiceSum).setEntryTime(new Date()).updateById();
		}
	}

	@Override
	public void recordCompanyInventoryOut(int tenThousand, int hundredThousand, int companyId, int invoiceType, int adminid,
	                                      int invoiceId) {
		// 更新发票库存
		CompanyInventory inventory = new CompanyInventory().selectById(companyId);
		if (inventory == null) return;

		int tenThousandInvoiceSum = 0;
		int hundredTHousandInvoiceSum = 0;
		if (invoiceType == InvoiceType.GENERAL) {
			// 普票
			tenThousandInvoiceSum = inventory.getGenTenThousand() - tenThousand;
			hundredTHousandInvoiceSum = inventory.getGenHundredThousand() - hundredThousand;
			inventory.setGenTenThousand(tenThousandInvoiceSum);
			inventory.setGenHundredThousand(hundredTHousandInvoiceSum);
		} else if (invoiceType == InvoiceType.SPECIAL) {
			// 专票
			tenThousandInvoiceSum = inventory.getSpeTenThousand() - tenThousand;
			hundredTHousandInvoiceSum = inventory.getSpeHundredThousand() - hundredThousand;
			inventory.setSpeTenThousand(tenThousandInvoiceSum);
			inventory.setSpeHundredThousand(hundredTHousandInvoiceSum);
		} else {
			return;
		}
		inventory.updateById();

		// 更新出库记录
		if (tenThousand > 0) {
			CompanyInventoryRecord record = new CompanyInventoryRecord().setCompanyId(companyId)
					.setAdminid(adminid)
					.setEntryTime(new Date())
					.setInvoiceType(invoiceType)
					.setInvoiceNum(tenThousand)
					.setAmountType(1)
					.setInvoiceId(invoiceId)
					.setInvoiceSum(tenThousandInvoiceSum)
					.setState(3);

			record.insert();
		}
		if (hundredThousand > 0) {
			CompanyInventoryRecord record = new CompanyInventoryRecord().setCompanyId(companyId)
					.setAdminid(adminid)
					.setEntryTime(new Date())
					.setInvoiceType(invoiceType)
					.setInvoiceNum(hundredThousand)
					.setAmountType(2)
					.setInvoiceId(invoiceId)
					.setInvoiceSum(hundredTHousandInvoiceSum)
					.setState(3);

			record.insert();
		}
	}
}
